﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AGuang.WebTool.Controls {
    public class NRepeater:Repeater {
        private Object dataSorece;
        private int recordCount;
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize {
            get {
                Object obj = ViewState["PageSize"];
                if (obj == null) return 20;
                else return (int)obj;
            }
            set { ViewState["PageSize"] = value; }
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int CurrentPage {
            get {
                Object obj = ViewState["CurrentPage"];
                if (obj == null) return 20;
                else return (int)obj;
            }
            set { ViewState["CurrentPage"] = value; }
        }

        /// <summary>
        /// 页面记录数
        /// </summary>
        public int RecordCount {
            get {
                return recordCount;
            }
        }

        /// <summary>
        /// 数据源
        /// </summary>
        public Object DataSource {
            get {
                return dataSorece;
            }
            set {
                dataSorece = value;
                if (dataSorece is DataTable) recordCount = ((DataTable)dataSorece).DefaultView.Count;
                else if (dataSorece is DataView) recordCount = ((DataView)dataSorece).Count;
                else if (dataSorece is IList) recordCount=((IList)dataSorece).Count;
            }
        }

        public override void DataBind() {
            int start = (CurrentPage - 1) * PageSize;
            int end = start + PageSize;

            if (dataSorece is DataTable) {
                DataTable ds = (DataTable)dataSorece;
                DataTable dt = new DataTable();
                foreach (DataColumn col in ds.Columns) {
                    dt.Columns.Add(col.ColumnName);
                }
                for (int i = start; i < end; i++) {
                    if (i >= ds.Rows.Count) break;
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn col in ds.Columns) {
                        dr[col.ColumnName] = ds.Rows[i][col.ColumnName];
                    }
                    dt.Rows.Add(dr);
                }
                base.DataSource = dt;
            } else if (dataSorece is DataView) {
                DataView ds = (DataView)dataSorece;
                DataTable dt = new DataTable();
                foreach (DataColumn col in ds.Table.Columns) {
                    dt.Columns.Add(col.ColumnName);
                }
                for (int i = start; i < end; i++) {
                    if (i >= ds.Count) break;
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn col in ds.Table.Columns) {
                        dr[col.ColumnName] = ds[i][col.ColumnName];
                    }
                    dt.Rows.Add(dr);
                }
                base.DataSource = dt;
            }

            base.DataBind();
        }
    }
}
